
local fname regressions

global repodir = substr("`c(pwd)'",1,length("`c(pwd)'") - (strpos(reverse("`c(pwd)'"),reverse("ipn"))) + 1)

capture log close
log using "$repodir/output/logs/`fname'.txt", replace text
display "$repodir"
do $repodir/code/setup.do

insheet using $dropbox/Data_outputted/E_Analysis/oems_panel_regressions.csv, clear
rename *, lower // replace capitalized variable names with lowercase
egen long panelid = group(oem_level1_id) 
sort panelid year
xtset panelid year


label variable count "Total Count"
label variable count_ice "ICE Count"
label variable count_effice "ICE Efficiency Count"
label variable count_cleancar "Clean Car Count"
label variable count_ev "EV Count"
label variable count_enab "Enabling Count"
label variable count_stor "Storage Count"
label variable count_bat "Battery Count"
label variable count_fc "Fuel Cell Count"
label variable stock "Total Stock"
label variable stock_cleancar "Clean Car Stock"
label variable stock_ice "ICE Stock"
label variable stock_effice "ICE Efficiency Stock"
label variable share_cleancar "Clean Car Share"
label variable ratio_cleancar_v_ice "Ratio Clean Car / ICE and ICE Eff"
label variable share_bat "Share Battery (in Clean Car)"
label variable share_fc "Share FC (in Clean Car)"
label variable ratio_bat_fc "Ratio Battery over Fuel Cell"
label variable cumulativeageoflink_mean "Mean Age Link"
label variable diff_share_bat_fc "Difference between Share of Battery and FC"

bys panelid (year): gen rddfocus = (otherstorage_rdexposure - fchydrogen_rdexposure)/(otherstorage_rdexposure + fchydrogen_rdexposure)

replace fchydrogen_rdexposure = fchydrogen_rdexposure/1000 
replace otherstorage_rdexposure = otherstorage_rdexposure/1000 
replace otherstorageiea_rdexposure = otherstorageiea_rdexposure/1000 
replace otherstoragesmooth_rdexposure = otherstoragesmooth_rdexposure/1000 
replace otherstoragesmooth2_rdexposure = otherstoragesmooth2_rdexposure/1000 

label variable otherstorage_rdexposure "BEV R\&D"
label variable otherstorageiea_rdexposure "BEV R\&D"
label variable otherstoragesmooth_rdexposure "BEV R\&D"
label variable otherstoragesmooth2_rdexposure "BEV R\&D"
   
*****************
* SUM STATS supply chains
*****************
label variable nbrsupp_all "Nbr of suppliers connected to carmaker"
label variable nbrsupp "Nbr of suppliers (from relevant industries) connected to carmaker"
label variable nbrsupplinks_mean "Nbr of links that the average supplier of the carmaker has"
label variable nbrsupplinks_mean_weighted_by_ag "Nbr of links that the average supplier of the carmaker has (weighted by age)"
label variable percentshared_by10 "Percent of suppliers shared by 10+ carmakers (\%)"
label variable cumulativeageoflink_mean "Age of the link between carmaker and its mean supplier"


eststo clear
quietly estpost sum ///
nbrsupp_all nbrsupp nbrsupplinks_mean nbrsupplinks_mean_weighted_by_ag percentshared_by10 cumulativeageoflink_mean
esttab using "$repodir/output/tables/sumstats_network.tex", replace nonum ///
cells("count mean(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2))") ///
noobs label booktabs

 
*********************************************************************
* GENERATING LAGS, LEADS AND SHOCKS
*********************************************************************

bys panelid (year): gen strat_fc_exposure_lag1 = strat_fc_exposure[_n-1]
label variable strat_fc_exposure_lag1 "FC Orientation t-1"
bys panelid (year): gen strat_fc_exposure_lag2 = strat_fc_exposure[_n-2]
label variable strat_fc_exposure_lag2 "FC Orientation t-2"
bys panelid (year): gen strat_fc_exposure_lag3 = strat_fc_exposure[_n-3]
label variable strat_fc_exposure_lag3 "FC Orientation t-3"

bys panelid (year): gen strat_fc_exposure_lead1 = strat_fc_exposure[_n+1]
label variable strat_fc_exposure_lead1 "FC Orientation t+1"
bys panelid (year): gen strat_fc_exposure_lead2 = strat_fc_exposure[_n+2]
label variable strat_fc_exposure_lead2 "FC Orientation t+2"
bys panelid (year): gen strat_fc_exposure_lead3 = strat_fc_exposure[_n+3]
label variable strat_fc_exposure_lead3 "FC Orientationt+3"
label variable strat_fc_exposure "FC Orientation"


bys panelid (year): gen change_strat_fc_exposure  = strat_fc_exposure[_n] - strat_fc_exposure[_n-1]
label variable change_strat_fc_exposure "Change of FC Strategy"
bys panelid (year): gen change_strat_fc_exposure_lag1  = change_strat_fc_exposure[_n-1]  
label variable change_strat_fc_exposure_lag1 "Change of FC Strategy t-1"
bys panelid (year): gen change_strat_fc_exposure_lead1  = change_strat_fc_exposure[_n+1]  
label variable change_strat_fc_exposure_lead1 "Change of FC Strategy t+1"
bys panelid (year): gen change_strat_fc_exposure_lead2  = change_strat_fc_exposure[_n+2]  
label variable change_strat_fc_exposure_lead2 "Change of FC Strategy t+2"

bys panelid (year): gen fchydrogen_lag1 = fchydrogen_rdexposure[_n-1]
label variable fchydrogen_lag1 "FC R\&D t-1"
bys panelid (year): gen fchydrogen_lag2 = fchydrogen_rdexposure[_n-2]
label variable fchydrogen_lag2 "FC R\&D t-2"
bys panelid (year): gen fchydrogen_lag3 = fchydrogen_rdexposure[_n-3]
label variable fchydrogen_lag3 "FC R\&D t-3"


bys panelid (year): gen fchydrogen_lead1 = fchydrogen_rdexposure[_n+1]
label variable fchydrogen_lead1 "FC R\&D t+1"
bys panelid (year): gen fchydrogen_lead2 = fchydrogen_rdexposure[_n+2]
label variable fchydrogen_lead2 "FC R\&D t+2"
bys panelid (year): gen fchydrogen_lead3 = fchydrogen_rdexposure[_n+3]
label variable fchydrogen_lead3 "FC R\&D t+3"
label variable fchydrogen_rdexposure "FC R\&D"

bys panelid (year): gen change_fchydrogen  = fchydrogen_rdexposure[_n] - fchydrogen_rdexposure[_n-1]
label variable change_fchydrogen "Change in FC R\&D"
bys panelid (year): gen change_fchydrogen_lag1  = change_fchydrogen[_n-1]  
label variable change_fchydrogen_lag1 "Change in FC R\&D t-1"
bys panelid (year): gen change_fchydrogen_lead1  = change_fchydrogen[_n+1]  
label variable change_fchydrogen_lead1 "Change in FC R\&D t+1"

bys panelid (year): gen strat_bev_exposure_lag1 = strat_bev_exposure[_n-1]
label variable strat_bev_exposure_lag1 "BEV Orientation t-1"
bys panelid (year): gen strat_bev_exposure_lag2 = strat_bev_exposure[_n-2]
label variable strat_bev_exposure_lag2 "BEV Orientation t-2"
bys panelid (year): gen strat_bev_exposure_lag3 = strat_bev_exposure[_n-3]
label variable strat_bev_exposure_lag3 "BEV Orientation t-3"

bys panelid (year): gen strat_bev_exposure_lead1 = strat_bev_exposure[_n+1]
label variable strat_bev_exposure_lead1 "BEV Orientation t+1"
bys panelid (year): gen strat_bev_exposure_lead2 = strat_bev_exposure[_n+2]
label variable strat_bev_exposure_lead2 "BEV Orientation t+2"
bys panelid (year): gen strat_bev_exposure_lead3 = strat_bev_exposure[_n+3]
label variable strat_bev_exposure_lead3 "BEV Orientation t+3"
label variable strat_bev_exposure "BEV Orientation"


bys panelid (year): gen change_strat_bev_exposure  = strat_bev_exposure[_n] - strat_bev_exposure[_n-1]
label variable change_strat_bev_exposure "Change of BEV Orientation"
bys panelid (year): gen change_strat_bev_exposure_lag1  = change_strat_bev_exposure[_n-1] 
label variable change_strat_bev_exposure_lag1 "Change of BEV Orientation t-1"
bys panelid (year): gen change_strat_bev_exposure_lead1  = change_strat_bev_exposure[_n+1]  
label variable change_strat_bev_exposure_lead1 "Change of BEV Orientation t+1"
bys panelid (year): gen change_strat_bev_exposure_lead2  = change_strat_bev_exposure[_n+2]  
label variable change_strat_bev_exposure_lead2 "Change of BEV Orientation t+2"

bys panelid (year): gen otherstorage_lag1 = otherstorage_rdexposure[_n-1]
label variable otherstorage_lag1 "BEV R\&D t-1"
bys panelid (year): gen otherstorage_lag2 = otherstorage_rdexposure[_n-2]
label variable otherstorage_lag2 "BEV R\&D t-2"
bys panelid (year): gen otherstorage_lag3 = otherstorage_rdexposure[_n-3]
label variable otherstorage_lag3 "BEV R\&D t-3"


bys panelid (year): gen otherstorage_lead1 = otherstorage_rdexposure[_n+1]
label variable otherstorage_lead1 "BEV R\&D t+1"
bys panelid (year): gen otherstorage_lead2 = otherstorage_rdexposure[_n+2]
label variable otherstorage_lead2 "BEV R\&D t+2"
bys panelid (year): gen otherstorage_lead3 = otherstorage_rdexposure[_n+3]
label variable otherstorage_lead3 "BEV R\&D t+3"

bys panelid (year): gen change_otherstorage = otherstorage_rdexposure[_n] - otherstorage_rdexposure[_n-1]
label variable change_otherstorage "Change in BEV R\&D"
bys panelid (year): gen change_otherstorage_lag1  =  change_otherstorage[_n-1]
label variable change_otherstorage_lag1 "Change in BEV R\&D t-1"
bys panelid (year): gen change_otherstorage_lead1  = change_otherstorage[_n+1] 
label variable change_otherstorage_lead1 "Change in BEV R\&D t+1"


* other measure of RD
* otherstorageiea_rdexposure
bys panelid (year): gen otherstorageiea_lag1 = otherstorageiea_rdexposure[_n-1]
label variable otherstorageiea_lag1 "BEV R\&D t-1"

bys panelid (year): gen otherstorageiea_lead1 = otherstorageiea_rdexposure[_n+1]
label variable otherstorageiea_lead1 "BEV R\&D t+1"

bys panelid (year): gen change_otherstorageiea = otherstorageiea_rdexposure[_n] - otherstorageiea_rdexposure[_n-1]
label variable change_otherstorageiea "Change in BEV R\&D"

bys panelid (year): gen change_otherstorageiea_lag1 = change_otherstorageiea[_n-1]
label variable change_otherstorageiea_lag1 "Change in BEV R\&D t-1"

bys panelid (year): gen change_otherstorageiea_lead1 = change_otherstorageiea[_n+1]  
label variable change_otherstorageiea_lead1 "Change in BEV R\&D t+1"

* otherstoragesmooth_rdexposure
bys panelid (year): gen otherstoragesmoo_lag1 = otherstoragesmooth_rdexposure[_n-1]
label variable otherstoragesmoo_lag1 "BEV R\&D t-1"

bys panelid (year): gen otherstoragesmoo_lead1 = otherstoragesmooth_rdexposure[_n+1]
label variable otherstoragesmoo_lead1 "BEV R\&D t+1"

bys panelid (year): gen change_otherstoragesmoo = otherstoragesmooth_rdexposure[_n] - otherstoragesmooth_rdexposure[_n-1]
label variable change_otherstoragesmoo "Change in BEV R\&D"

bys panelid (year): gen change_otherstoragesmoo_lag1 = change_otherstoragesmoo[_n-1]
label variable change_otherstoragesmoo_lag1 "Change in BEV R\&D t-1"

bys panelid (year): gen change_otherstoragesmoo_lead1 = change_otherstoragesmoo[_n+1]  
label variable change_otherstoragesmoo_lead1 "Change in BEV R\&D t+1"

   
* otherstoragesmooth2_rdexposure
bys panelid (year): gen otherstoragesmoo2_lag1 = otherstoragesmooth2_rdexposure[_n-1]
label variable otherstoragesmoo2_lag1 "BEV R\&D t-1"

bys panelid (year): gen otherstoragesmoo2_lead1 = otherstoragesmooth2_rdexposure[_n+1]
label variable otherstoragesmoo2_lead1 "BEV R\&D t+1"

bys panelid (year): gen change_otherstoragesmoo2 = otherstoragesmooth2_rdexposure[_n] - otherstoragesmooth2_rdexposure[_n-1]
label variable change_otherstoragesmoo2 "Change in BEV R\&D"

bys panelid (year): gen change_otherstoragesmoo2_lag1 = change_otherstoragesmoo2[_n-1]
label variable change_otherstoragesmoo2_lag1 "Change in BEV R\&D t-1"

bys panelid (year): gen change_otherstoragesmoo2_lead1 = change_otherstoragesmoo2[_n+1]  
label variable change_otherstoragesmoo2_lead1 "Change in BEV R\&D t+1"


***** MAIN REGRESSIONS *****
gen beg_year = 2000
gen end_year = 2015
***** Leads and lags together *********
***** Each orientation policy separately *********



capture program drop OuputRegTable
program define OuputRegTable, rclass
	eststo clear 
	eststo: reghdfe `4' `1' if inrange(year, beg_year, end_year), noabsorb vce(cluster panelid)
	estadd local Firm_Clusters `e(N_clust1)' , replace

	eststo: reghdfe `4' `1' if inrange(year, beg_year, end_year), absorb(i.panelid) vce(cluster panelid)
	estadd local Firm "X" , replace
	estadd local Firm_Clusters `e(N_clust1)' , replace

	eststo: reghdfe `4' `1' if inrange(year, beg_year, end_year), absorb(i.year) vce(cluster panelid)
	estadd local Year "X" , replace
	estadd local Firm_Clusters `e(N_clust1)' , replace

	eststo: reghdfe `4' `1' if inrange(year, beg_year, end_year), absorb(i.panelid i.year) vce(cluster panelid)
	estadd local Year "X" , replace
	estadd local Firm "X" , replace
	estadd local Firm_Clusters `e(N_clust1)' , replace

	eststo: reghdfe `4' `2' if inrange(year, beg_year, end_year), noabsorb vce(cluster panelid)
	estadd local Firm_Clusters `e(N_clust1)' , replace

	eststo: reghdfe `4' `2' if inrange(year, beg_year, end_year), absorb(i.panelid) vce(cluster panelid)
	estadd local Firm "X" , replace
	estadd local Firm_Clusters `e(N_clust1)' , replace

	eststo: reghdfe `4' `2' if inrange(year, beg_year, end_year), absorb(i.year) vce(cluster panelid)
	estadd local Year "X" , replace
	estadd local Firm_Clusters `e(N_clust1)' , replace

	eststo: reghdfe `4' `2' if inrange(year, beg_year, end_year), absorb(i.panelid i.year) vce(cluster panelid)
	estadd local Year "X" , replace
	estadd local Firm "X" , replace
	estadd local Firm_Clusters `e(N_clust1)' , replace

	eststo: reghdfe `4' `1' `2' if inrange(year, beg_year, end_year), noabsorb vce(cluster panelid)
	estadd local Firm_Clusters `e(N_clust1)' , replace

	eststo: reghdfe `4' `1' `2' if inrange(year, beg_year, end_year), absorb(i.panelid) vce(cluster panelid)
	estadd local Firm "X" , replace
	estadd local Firm_Clusters `e(N_clust1)' , replace

	eststo: reghdfe `4' `1' `2' if inrange(year, beg_year, end_year), absorb(i.year) vce(cluster panelid)
	estadd local Year "X" , replace
	estadd local Firm_Clusters `e(N_clust1)' , replace

	eststo: reghdfe `4' `1' `2' if inrange(year, beg_year, end_year), absorb(i.panelid i.year) vce(cluster panelid)
	estadd local Year "X" , replace
	estadd local Firm "X" , replace
	estadd local Firm_Clusters `e(N_clust1)' , replace

	local labelx : variable label `4'
	** Table lags only Orientation policies
	esttab using "$repodir/output/tables/regtable_policies_`4'_`3'.tex", replace b(%9.2f) ///
		stats(Year Firm Firm_Clusters r2 N, ///
		fmt(%~12s %~12s %9.3gc %9.2f %12.3gc) ///
		label("Year FEs" "Firm FEs" "Firm Clusters (SEs)" "R2" "Observations")) ///
		booktabs eqlabels(none) noconstant noomitted se label ///
		star(* 0.10 ** 0.05 *** 0.01)  ///
		nomtitles noomitted ///
		nonotes addnote ( "Dependent variable: `labelx'. " /// 
		"OLS. Cluster-robust standard errors in parentheses. * p$<$0.10, ** p$<$0.05, *** p$<$0.01")
end


global FC_Orient strat_fc_exposure_lag1 strat_fc_exposure strat_fc_exposure_lead1 
global BEV_Orient strat_bev_exposure_lag1 strat_bev_exposure strat_bev_exposure_lead1 
global FC_RDD fchydrogen_lag1 fchydrogen_rdexposure fchydrogen_lead1 
global BEV_RDD otherstoragesmoo2_lag1 otherstoragesmooth2_rdexposure otherstoragesmoo2_lead1 


global BEV_RDD otherstorage_lag1 otherstorage_rdexposure otherstorage_lead1 

// SHARE BAT - SHARE FC

OuputRegTable "$FC_Orient" "$BEV_Orient" "orient_LEAD1LAG1" "diff_share_bat_fc"

OuputRegTable "$FC_RDD" "$BEV_RDD" "rdd_LEAD1LAG1" "diff_share_bat_fc"


// FC PATENTING
OuputRegTable "$FC_Orient" "$BEV_Orient" "orient_LEAD1LAG1" "count_fc"

OuputRegTable "$FC_RDD" "$BEV_RDD" "rdd_LEAD1LAG1" "count_fc"


// BAT PATENTING
OuputRegTable "$FC_Orient" "$BEV_Orient" "orient_LEAD1LAG1" "count_bat"

OuputRegTable "$FC_RDD" "$BEV_RDD" "rdd_LEAD1LAG1" "count_bat"


// CHANGES
cap drop change_count_fc change_count_bat
bys panelid (year): gen change_count_fc = count_fc_excl - count_fc_excl[_n-1]
bys panelid (year): gen change_count_bat = count_bat_excl - count_bat_excl[_n-1]

global FC_Orient change_strat_fc_exposure_lag1 change_strat_fc_exposure change_strat_fc_exposure_lead1
global BEV_Orient change_strat_bev_exposure_lag1 change_strat_bev_exposure change_strat_bev_exposure_lead1
global FC_RDD change_fchydrogen_lag1 change_fchydrogen change_fchydrogen_lead1
global BEV_RDD change_otherstoragesmoo2_lag1 change_otherstoragesmoo2 change_otherstoragesmoo2_lead1

global BEV_RDD change_otherstorage_lag1 change_otherstorage change_otherstorage_lead1 


OuputRegTable "$FC_Orient" "$BEV_Orient" "orient_LEAD1LAG1" "change_count_fc"

OuputRegTable "$FC_RDD" "$BEV_RDD" "rdd_LEAD1LAG1" "change_count_fc"

OuputRegTable "$FC_Orient" "$BEV_Orient" "orient_LEAD1LAG1" "change_count_bat"

OuputRegTable "$FC_RDD" "$BEV_RDD" "rdd_LEAD1LAG1" "change_count_bat"



capture program drop OuputCoeffPlot
program define OuputCoeffPlot, rclass
	eststo clear 
	eststo: reghdfe diff_share_bat_fc `1' if inrange(year, beg_year, end_year), absorb(i.panelid)  vce(cluster panelid)
	estadd local Firm_Clusters `e(N_clust1)' , replace
	coefplot, keep(`2') vertical yline(0, lcolor("black"))   ///
	xtitle("Exposure to `3'" , size(huge)) ytitle("Share of Battery Minus Share of FC") ///
	xlab(1 "t-1" 2 "t" 3 "t+1", labsize(medium)) ///
	graphregion(color(white)) ///
	msymbol(d) mcolor(`4') lcolor(`4') ciopts(lcolor(`4')) 
	graph export "$repodir/output/figures/coeffplots/coeffplot`5'.png", replace
	graph close
end 


global CovariatesOrientation  strat_bev_exposure_lag1 strat_bev_exposure strat_bev_exposure_lead1  strat_fc_exposure_lag1 strat_fc_exposure strat_fc_exposure_lead1
global varforcoeffpol strat_bev_exposure_lag1 strat_bev_exposure strat_bev_exposure_lead1 
OuputCoeffPlot "$CovariatesOrientation" "$varforcoeffpol" "BEV Orientation" "blue" "BEVorientation"
global varforcoeffpol strat_fc_exposure_lag1 strat_fc_exposure strat_fc_exposure_lead1
OuputCoeffPlot "$CovariatesOrientation" "$varforcoeffpol"  "FC Orientation" "orange" "FCorientation"

global CovariatesRDD otherstorage_lag2 otherstorage_lag1 otherstorage_rdexposure  fchydrogen_lag2 fchydrogen_lag1 fchydrogen_rdexposure 
global varforcoeffpol otherstorage_lag2 otherstorage_lag1 otherstorage_rdexposure 
OuputCoeffPlot "$CovariatesRDD" "$varforcoeffpol"  "BEV RD&D" "blue" "BEVrdd"
global varforcoeffpol fchydrogen_lag2 fchydrogen_lag1 fchydrogen_rdexposure 
OuputCoeffPlot "$CovariatesRDD" "$varforcoeffpol"  "FC RD&D" "orange" "FCrdd"





do $repodir/Code/closeout.do
exit

